home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '88 / Other stuff / MPS communications / LSC9 demo / LSCdemo9.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-10-21  |  13.7 KB  |  540 lines  |  [TEXT/KAHL]

  1. /*                    Application C template source file    
  2. David A. Feldt                     - version 1.2 -                        8/12/85
  3.  
  4.               Copyright © 1985 by David A. Feldt.  All rights reserved.
  5.  
  6. This template is based on a Pascal example program written by David Wilson of Personal
  7. Concepts and distributed to students of Apple's Macintosh Technical Training course.*/
  8.  
  9. #include        <QuickDraw.h>
  10. #include        <MenuMgr.h>
  11. #include        <WindowMgr.h>
  12. #include        <TextEdit.h>
  13. #include        <EventMgr.h>
  14. #include        <DeskMgr.h>
  15. #include        <ControlMgr.h>
  16. #include        <ResourceMgr.h>
  17. #include        <FontMgr.h>
  18. #include        <DialogMgr.h>
  19. #include        <PackageMgr.h>
  20. #include        <PrintMgr.h>
  21. #include        <MemoryMgr.h>
  22. #include        <OSUtil.h>
  23. #include        <ToolboxUtil.h>
  24. #include        <FileMgr.h>
  25. #include        <StdFilePkg.h>
  26.  
  27. #define        pass(A)        (A)
  28. #define        RETURN        return
  29.  
  30. #define        NULL            0L                  /* 32 bit  0 or hex 0x0000          */
  31. #define        NEG_ONE        1L
  32.  
  33. #define    MAXMENUS        7                    /* maximum number of menus    */
  34. #define    APPLEMENU        401
  35. #define    APPLE        0
  36. #define    FILEMENU        402                    /* resource id                */
  37. #define    FILE            1
  38. #define     FILENEW        1                    /* item number                */
  39. #define    FILEQUIT        8
  40. #define    EDITMENU        403
  41. #define    EDIT           2
  42. #define    EDITCUT        3
  43. #define    EDITCOPY        4
  44. #define    EDITPASTE        5
  45. #define    EDITSHOW        7
  46. #define    Font            3
  47. #define    fontID        404
  48. #define    Size            4
  49. #define    sizeID        405
  50. #define    Pen            5
  51. #define    penID        406
  52. #define    penSmall        1
  53. #define    penMedium        2
  54. #define    penLarge        3
  55. #define    penBlack        5
  56. #define    penGray        6
  57. #define    penWhite        7
  58. #define    Picture        6
  59. #define    pictureID        407
  60. #define    SKETCH        1
  61. #define    RECTANGLE        2
  62. #define    OVAL            3
  63. #define    TEXT            4
  64. #define    windowID        401
  65. #define    DIALOG_ID        401
  66. #define    CURSOR_ID        401
  67. #define    iBeamCursor    1                    /* system resource */
  68.  
  69.         /* ----------------- global variables --------------------------*/
  70.         
  71. GrafPtr            tempPort;                    /* store current port during updates */
  72. WindowPtr            theWindow, eventWindow;
  73. WindowRecord        wRecord;
  74. MenuHandle        myMenus[MAXMENUS];            /* array to hold menu handles */
  75. Rect                windowRect, dragRect,textRect;
  76. char                doneflag, temp;            /* boolean */
  77. EventRecord        myEvent;                    /* pointer to event record */
  78. int                theMenu, theItem, drawMode, lastMode;
  79. char                ch;
  80. CursHandle        drawHandle,iBeamHdl;
  81. TEHandle            hTE;                        /* handle to text edit record */
  82. PicHandle            picHdl;                    /* picture handle for graphics updates */
  83.  
  84.         /* --------------------  Main Program --------------------------*/
  85.         
  86. main()
  87. {
  88.     init_stuff();                            /* program initialization routine */
  89.     
  90.         /*  ---------------------- main event loop --------------------- */ 
  91.         
  92.     while (!doneflag)    {                /* continue until quit is selected        */
  93.     SystemTask();
  94.     if (theWindow == FrontWindow())        /* if our window is in front (active)     */
  95.         Cursor_adjust();                /* change to the appropriate cursor    */
  96.     if (drawMode == TEXT) TEIdle(hTE);        /* blink cursor if entering text        */
  97.     if(GetNextEvent(everyEvent, &myEvent))          
  98.         switch (myEvent.what) {
  99.  
  100.             case mouseDown:
  101.                 Handle_mouse_down();
  102.                 break;
  103.  
  104.             case keyDown:
  105.             case autoKey:                  
  106.                 ch = (int)(myEvent.message & charCodeMask);
  107.                 if (myEvent.modifiers & cmdKey) 
  108.                     do_command(MenuKey(ch));    /* may be a command key menu equivalent */
  109.                 else
  110.                     if (theWindow == FrontWindow()) {
  111.                         if (drawMode == TEXT) 
  112.                             TEKey(ch, hTE);
  113.                         else 
  114.                             DrawChar(ch);
  115.                     }
  116.                 break;
  117.  
  118.             case activateEvt:
  119.                 if (myEvent.modifiers & 1) {         /* if an activate event    */
  120.                     SetPort(myEvent.message);
  121.                     TEActivate(hTE);
  122.                 }
  123.                 else                            /* if a deactivate event */ 
  124.                     TEDeactivate(hTE);
  125.                 break;
  126.            
  127.             case updateEvt:             
  128.                 Handle_update(myEvent.message);
  129.                 break;
  130.         }  
  131.     }  
  132. }
  133.  
  134.         /* -----------  do file menu commands ------------------------ */
  135.         
  136. do_file_commands(fileItem)
  137. int fileItem;
  138. {
  139.     switch(fileItem) {
  140.      
  141.         case FILENEW:
  142.             SelectWindow(theWindow);            /* bring window to the front     */
  143.             ShowWindow(theWindow);
  144.             EraseRect(&theWindow->portRect);
  145.             break;
  146.          
  147.         case FILEQUIT: 
  148.             doneflag = TRUE;                /* exit main event loop         */
  149.             break;
  150.     }
  151. }
  152.  
  153.         /* -----------  do Edit menu commands ------------------------ */
  154.         
  155. do_edit_commands(editItem)
  156. int editItem;
  157. {
  158.     if (!SystemEdit(editItem-1)) {            /* if a desk accessory cut, copy or paste    */
  159.         if (theWindow == FrontWindow())         /* it will be handles and this code skipped    */
  160.             switch(editItem) {
  161.                 case EDITCUT:          
  162.                     if (drawMode == TEXT) TECut(hTE);
  163.                     break;
  164.                 case EDITCOPY:     
  165.                     if (drawMode == TEXT) TECopy(hTE);                    
  166.                     break;
  167.                 case EDITPASTE:     
  168.                     if (drawMode == TEXT) TEPaste(hTE);
  169.                     break;
  170.                 case EDITSHOW:
  171.                     break;
  172.         }     
  173.     }
  174. }
  175.  
  176.         /* -------------------- Handle Font menu commands ----------------------*/
  177.     
  178. do_font_commands(fontItem)
  179. int fontItem;
  180. {
  181.     char        fontName[255];
  182.     int        fontNum;
  183.         
  184.      GetItem(myMenus[Font], fontItem, fontName);
  185.     GetFNum(fontName, &fontNum);
  186.     TextFont(fontNum);
  187.     (**hTE).txFont = fontNum;
  188. }
  189.  
  190.         /* -------------------- Handle Size menu commands ----------------------*/
  191.     
  192. do_size_commands(sizeItem)
  193. int sizeItem;
  194. {
  195.     char        sizeString[255];
  196.     long        sizeNumber;
  197.     
  198.     GetItem(myMenus[Size], sizeItem, sizeString);
  199.     StringToNum(sizeString, &sizeNumber);
  200.     TextSize(LoWord(sizeNumber));
  201.     (**hTE).txSize = LoWord(sizeNumber);
  202. }
  203.  
  204.         /* -------------------- Handle Pen menu commands ----------------------*/
  205.     
  206. do_pen_commands(penItem)
  207. int penItem;
  208. {
  209.     switch(penItem) {
  210.     
  211.         case penSmall:
  212.             PenSize(1,1);    
  213.             break;
  214.         case penMedium:
  215.             PenSize(6,6);    
  216.             break;
  217.         case penLarge:
  218.             PenSize(20,20);
  219.             break;
  220.         case penBlack:        
  221.             PenPat(black);
  222.             break;
  223.         case penGray:      
  224.             PenPat(gray);    
  225.             break;
  226.         case penWhite:      
  227.             PenPat(white);    
  228.             break;
  229.     }
  230. }
  231.  
  232.         /* --------------- Handle Picture menu commands -------------------*/
  233.         
  234. do_Pict_commands(pictItem)
  235. int pictItem;
  236. {
  237.     drawMode = pictItem;
  238.     CheckItem(myMenus[Picture],lastMode,FALSE);    /* remove check from old item */
  239.     CheckItem(myMenus[Picture],pictItem,TRUE);    /* add check to new item */
  240.     lastMode = pictItem;
  241. }
  242.  
  243.         /* ----------- Handle menu (and key equivalent) commands -------*/
  244.     
  245. do_command(theMenu, theItem)
  246. int theMenu, theItem;
  247.  
  248. {
  249.     char name[255];                        /* a string of 255 characters     */
  250.     int i;
  251.  
  252.     switch (theMenu) {
  253.     
  254.     case APPLEMENU:
  255.         if (theWindow == FrontWindow())
  256.             save_screen();
  257.         if (theItem == 1) show_dialog();        /* if "about demo ..." selected     */
  258.         else {
  259.             GetItem(myMenus[APPLE], theItem, name);/* get the desk accessory's name     */
  260.             OpenDeskAcc(name);                /* open the desk accessory        */
  261.         }
  262.         break;
  263.         
  264.     case FILEMENU: 
  265.         do_file_commands(theItem); 
  266.         break;
  267.          
  268.     case EDITMENU:
  269.         do_edit_commands(theItem);
  270.         break;
  271.  
  272.     case fontID:
  273.         do_font_commands(theItem);
  274.         break;
  275.  
  276.     case sizeID:
  277.         do_size_commands(theItem);
  278.         break;
  279.  
  280.     case penID:
  281.         do_pen_commands(theItem);
  282.         break;
  283.  
  284.     case pictureID:
  285.         do_Pict_commands(theItem);
  286.         break;
  287.     }
  288.     HiliteMenu(0);
  289. }
  290.  
  291.         /* -------------- sketch in window when mouse down ------------ */
  292.         
  293. draw_lines()
  294. {
  295.     Point    p1,p2;
  296.     
  297.     GetMouse(&p1);                            /* get current mouse position         */
  298.     MoveTo(p1.h, p1.v);                        /* position pen to mouse position         */
  299.     while (Button()) {                        /* until the mouse button is released     */
  300.         GetMouse(&p2);                        /* get new mouse position             */
  301.         if (!EqualPt(pass(p1),pass(p2))) {        /* if the two positions are different     */
  302.             LineTo(p2.h, p2.v);                /* draw a line between them            */
  303.             p1.h = p2.h;                    /* set old position to new position     */
  304.             p1.v = p2.v;
  305.         }
  306.     }        
  307. }
  308.  
  309.         /* ------- draw rectangles in window when mouse down -------- */
  310.         
  311. draw_Rect(startPt)
  312. Point    startPt;
  313. {
  314.     Point    pNew,pOld;
  315.     Rect        newRect,oldRect;
  316.     PenState    pnState;
  317.     
  318.     GetPenState(&pnState);                    /* save current pendata */
  319.     PenMode(patXor);                        /* for drawing temporary outline */
  320.     PenPat(dkGray);
  321.     pOld = startPt;
  322.     MoveTo(startPt.h, startPt.v);
  323.     do {
  324.         GetMouse(&pNew);
  325.         if (! EqualPt(pass(pNew),pass(pOld))) {
  326.             SetRect(&oldRect,startPt.h,startPt.v,pOld.h,pOld.v); /* define rect from two corners */
  327.             SetRect(&newRect,startPt.h,startPt.v,pNew.h,pNew.v); /* define rect from two corners */
  328.             FrameRect(&oldRect);            /* erase temporary rectangle */                
  329.             FrameRect(&newRect);            /* draw temporary rectangle */
  330.             pOld = pNew;
  331.         }
  332.     } while (Button() == TRUE);
  333.     SetPenState(&pnState);                    /* restore original pendata */
  334.     FrameRect(&newRect);                    /* draw final rectangle */    
  335. }
  336.  
  337.         /* ------- draw oval in window when mouse down -------- */
  338.         
  339. draw_oval()
  340. {
  341.     Point    p1,p2;
  342.     Rect        tempRect;
  343.     PenState    pnState;
  344.     
  345.     GetPenState(&pnState);                    /* save current pendata */
  346.     PenMode(patXor);                        /* for drawing temporary outline */
  347.     GetMouse(&p1);
  348.     MoveTo(p1.h, p1.v);
  349.     do {
  350.         GetMouse(&p2);
  351.         Pt2Rect(pass(p1),pass(p2),&tempRect);    /* define rectangle from two corners */
  352.         FrameOval(&tempRect);                /* draw temporary oval */                
  353.         FrameOval(&tempRect);                /* erase temporary oval */                
  354.     } while (Button() == TRUE);
  355.     SetPenState(&pnState);                    /* restore original pendata */
  356.     FrameOval(&tempRect);                    /* draw final oval */    
  357. }
  358.  
  359.         /* -------------- Handle all mouseDown events -------------------*/
  360.         
  361. Handle_mouse_down()
  362. {
  363.     WindowPtr mouseWindow;
  364.     int        location;
  365.     Point    mDown;    
  366.  
  367.         location = FindWindow(pass(myEvent.where), &mouseWindow);
  368.         switch(location) {
  369.  
  370.             case inMenuBar:                /* mouse down in pull down menu */
  371.                 do_command(MenuSelect(pass(myEvent.where)));
  372.                 break;
  373.  
  374.             case inSysWindow:                /* mousedown in desk accessory */
  375.                 SystemClick(&myEvent, mouseWindow); 
  376.                 break;
  377.  
  378.             case inGoAway:                    /* mouse down in go away box */
  379.                 if (TrackGoAway(theWindow,pass(myEvent.where))) 
  380.                     HideWindow(theWindow); 
  381.                 break;
  382.             
  383.             case inDrag:                    /* mouse down in title bar of window */
  384.                 if (mouseWindow != FrontWindow())
  385.                 SelectWindow(mouseWindow);
  386.                 DragWindow(mouseWindow, pass(myEvent.where), &dragRect);
  387.                 break;
  388.                                                  
  389.             case inContent:                /* mouse down in content region of window */
  390.                 if (mouseWindow != FrontWindow())
  391.                     SelectWindow(mouseWindow);
  392.                 if (theWindow == FrontWindow())
  393.                     mDown = myEvent.where;
  394.                     GlobalToLocal(&mDown);
  395.                     switch(drawMode) {
  396.                         case SKETCH:        
  397.                             draw_lines();    
  398.                             break;
  399.                         case RECTANGLE:    
  400.                             draw_Rect(pass(mDown));    
  401.                             break;
  402.                         case OVAL:        
  403.                             draw_oval();    
  404.                             break;
  405.                         case TEXT:
  406.                             TEClick(pass(mDown),0,hTE);
  407.                             break;
  408.                     }
  409.                 break;
  410.         }            
  411. }
  412.  
  413.  
  414.         /* ---------- update the window contents ----------------------*/
  415.         
  416. Handle_update(updateWindow)
  417. WindowPtr    updateWindow;
  418. {
  419.     GrafPtr    tempPort;
  420.     
  421.     GetPort(&tempPort);             /* save current grafport */
  422.     SetPort(updateWindow);
  423.     BeginUpdate(updateWindow);
  424.     if (drawMode == TEXT) {            /* currently text mode */
  425.         EraseRect(&theWindow->portRect);
  426.         TEUpdate(&textRect, hTE);
  427.     }
  428.     else {                        /* currently graphics mode */
  429.         HLock(picHdl);                /* lock picture so it doesn't move */
  430.         DrawPicture(picHdl,&textRect);        /* redraw screen contents */
  431.         HUnlock(picHdl);            /* set picture relocatable again */
  432.     }
  433.     EndUpdate(updateWindow);
  434.     SetPort(tempPort);                /* restore original grafport */
  435. }
  436.  
  437.         /* ------- adjust the Cursor when theWindow in front -------------*/
  438.         
  439. Cursor_adjust()
  440. {
  441.     Point     mousePt;
  442.     
  443.     GetMouse(&mousePt);
  444.     if (PtInRect(pass(mousePt), &theWindow->portRect))     /* if mouse in content */
  445.         switch(drawMode) {
  446.             case TEXT:
  447.                 SetCursor(&(**iBeamHdl));            /* use text cursor */
  448.                 break;
  449.             case SKETCH:
  450.             case RECTANGLE:
  451.             case OVAL:
  452.                 SetCursor(&(**drawHandle));            /* use graphics cursor */
  453.                 break;
  454.         }
  455.     else 
  456.         InitCursor();                                /* use arrow cursor       */
  457. }
  458.  
  459.         /* ------------------ initialize menus --------------------------*/
  460.         
  461. set_up_menus()
  462. {
  463.     InsertMenu(myMenus[APPLE] = GetMenu(APPLEMENU),0);
  464.     AddResMenu(myMenus[APPLE], 'DRVR');            /* get desk accessory names */
  465.     InsertMenu(myMenus[FILE] = GetMenu(FILEMENU),0);
  466.     InsertMenu(myMenus[EDIT] = GetMenu(EDITMENU),0);
  467.     InsertMenu(myMenus[Font] = GetMenu(fontID),0);
  468.     AddResMenu(myMenus[Font], 'FONT');
  469.     InsertMenu(myMenus[Size] = GetMenu(sizeID),0);
  470.     InsertMenu(myMenus[Pen] = GetMenu(penID),0);
  471.     InsertMenu(myMenus[Picture] = GetMenu(pictureID),0);
  472.     DrawMenuBar();
  473.     CheckItem(myMenus[Picture],TEXT,TRUE);            /* put check mark by text item */
  474.     lastMode = TEXT;                            /* set previous mode to text */
  475. }
  476.  
  477.         /* ---------------- modal dialog box -----------------------*/
  478.         
  479. show_dialog()
  480. {
  481.     DialogPtr        dp;
  482.     int            ditem;
  483.    
  484.     dp = GetNewDialog(DIALOG_ID,NULL,NEG_ONE);
  485.     ModalDialog(NULL,&ditem);
  486.     DisposDialog(dp);
  487. }
  488.  
  489.         /* ------------- save screen for graphics update --------------- */
  490.  
  491. save_screen()
  492. {
  493.     BitMap        map;
  494.     
  495.     KillPicture(picHdl);                /* reclaim and reuse pic memory in heap */
  496.     ClipRect(&screenBits.bounds);            /* limit clip region to size of screen */
  497.     map = theWindow->portBits;            /* the bitmap for the graphics/text window */
  498.     picHdl = OpenPicture(&textRect);        /* prepare to capture screen image */
  499.         CopyBits(&map,&map,&textRect,&textRect,1,NULL);    /* copy window to itself */
  500.     ClosePicture();                    /* stop accumulating picture */
  501. }
  502.  
  503. init_stuff()
  504. {
  505.     MoreMasters();                            /* make 64 more Master Pointers */
  506.     MoreMasters();                            /* make 64 more Master Pointers */
  507.     MoreMasters();                            /* make 64 more Master Pointers */
  508.     InitGraf(&thePort);
  509.     InitFonts();
  510.     InitWindows();
  511.     InitMenus();
  512.     TEInit();
  513.     InitDialogs(NULL);                        /* error handling proc goes here */
  514.     InitCursor();
  515.  
  516.     OpenResFile("\Pdemo9.rsrc");
  517.     set_up_menus();
  518.     
  519.     windowRect.left = screenBits.bounds.left + 8;    /* rect for main text window     */    
  520.     windowRect.top = screenBits.bounds.top + 40;
  521.     windowRect.right = screenBits.bounds.right - 8;
  522.     windowRect.bottom = screenBits.bounds.bottom - 8;
  523.     theWindow = NewWindow(NULL, &windowRect,"\PDemo in C", 1, 0, NEG_ONE, 1,  NULL);
  524.     SetPort(theWindow);
  525.              
  526.     dragRect = windowRect;                    /* for dragging windows around */
  527.     InsetRect(&dragRect,-4,-4);
  528.  
  529.     textRect = theWindow->portRect;            /* text edit stuff */
  530.     InsetRect(&textRect,6,10);                /* destination & view rectangles */
  531.     hTE = TENew(&textRect, &textRect);
  532.     
  533.     drawHandle = GetCursor(CURSOR_ID);            /* miscellaneous initializing */
  534.     iBeamHdl = GetCursor(iBeamCursor);
  535.     doneflag = FALSE;
  536.     FlushEvents(everyEvent, 0);
  537.     drawMode = TEXT;
  538.     picHdl = NULL;                            /* zero handle so first update */
  539. }                                        /* won't blow up the program */
  540.